package com.atgui.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.Random;

public class phoneCode {
    public static void main(String[] args) {
       verifyCode("18475235210");
       //getRedisCode("18475235210","424243");
    }

    public static String getCode(){
        Random random = new Random();
        String code="";
        for (int i = 0; i < 6; i++) {
            int rand = random.nextInt(10);
            code+=rand;
        }
        return code;
    }

    public static void verifyCode(String phone){
        Jedis jedis = new Jedis("192.168.195.170", 6379);
        String countKey="VerifyCode"+phone+"count";
        String codeKey="VerifyCode"+phone+"code";
        String count = jedis.get(codeKey);
        if(count==null){
            jedis.setex(countKey,24*60*60,"1");
        }else if(Integer.parseInt(count)<=2){
            jedis.incr(codeKey);
        }else if(Integer.parseInt(count)>2){
            System.out.println("今天的发送次数已经超过3次");
            jedis.close();
            return;
        }
        String vcode = getCode();
        jedis.setex(codeKey,120,vcode);
        jedis.close();
    }

    public static void getRedisCode(String phone,String code){
        Jedis jedis = new Jedis("192.168.195.170", 6379);
        String codeKey="VerifyCode"+phone+"code";
        String redisCode = jedis.get(codeKey);
        if(redisCode.equals(code)){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
        jedis.close();
    }
}
